﻿using Newtonsoft.Json;
using System;
using System.Data;
using System.Text;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal
{
    public partial class MessageBoard : PortalPage
    {


        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }

        public void LoadData()
        {
            BindData();
        }

        private void WriteList()
        {
            DataTable dt = PartyMessageBoard.GetListData(Convert.ToInt32(this.PageIndex),Convert.ToInt32(PageSize),null,1).Tables[0];

            dt.Columns.Add("CreateTime2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                dr["CreateTime2"] = Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd HH:mm");
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyMessageBoard.GetListData(0, 10, null,1).Tables[1].Rows[0][0].ToString());
            Response.End();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            /*if (!IsPostBack)
            {
                BindData();
            }*/

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();

            }
            if (!IsPostBack)
            {
                //CheckLogin(true);
                LoadData();
            }

        }

        public void BindData()
        {
            var pickerurl = String.Format("../app/Share/Picker/BranchSinglePicker.aspx?ids=<script>{0}</script>",
                hdBranchID.GetValueReference());
            ppBranch.OnClientTrigger2Click = Window1.GetSaveStateReference(hdBranchID.ClientID, ppBranch.ClientID)
                                              + Window1.GetShowReference(pickerurl);

            StringBuilder js = new StringBuilder();
            js.AppendFormat(@"
                ; var pickerClientID = '{0}';
                  var pickerHdClientID = '{1}';
                  F(pickerClientID).setValue('');
                  F(pickerHdClientID).setValue('');
", ppBranch.ClientID, hdBranchID.ClientID);
            ppBranch.OnClientTrigger1Click = js.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            PartyMessageBoard message = new PartyMessageBoard();
            message.ID = StringHelper.NewID;
            message.UserName = txtName.Text.Trim();
            message.MobileNo = txtMobileNo.Text.Trim();
            message.BranchID = ppBranch.Value;
            message.Message = txtSuggestion.Text.Trim();
            message.CreateTime = DateTime.Now;
            message.ModifyTime = DateTime.Now;

            if (IsLogin)
            {
                message.CreateBy = this.CurrentUser.LoginID;
                message.ModifyBy = this.CurrentUser.LoginID;
            }
            else
            {
                message.CreateBy = string.Empty;
                message.ModifyBy = string.Empty;
            }

            DB.PartyMessageBoards.Add(message);
            DB.SaveChanges();

            Response.Write("<script>alert('提交成功！');location.href=location.href;</script>");
        }
    }
}